package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.FeedBackBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.SwitchView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---反馈
 */
public class FeedbackActivity extends BaseActivity implements View.OnClickListener, SwitchView.OnStateChangedListener {
    private TextView title;
    private TextView tv_right;

    private SwitchView sw_bathroom, sw_apartment, sw_mountings, sw_kitchen, sw_else;
    private LinearLayout ll_bathroom;
    private RelativeLayout rl_apartment, rl_mountings, rl_kitchen, rl_else;

    private ImageView iv_img1, iv_img2, iv_img3;
    private TextView hasNum, hasSum;
    private String info_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        initData();

        initTitle();
        initView();
    }


    private void initData() {
        info_id = getIntent().getStringExtra("info_id");
    }

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("反馈");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("提交");
        tv_right.setTextColor(0xff008c99);
    }

    private void initView() {
        et_bathroom_type = (EditText) findViewById(R.id.et_bathroom_type);
        et_kitchen_type = (EditText) findViewById(R.id.et_kitchen_type);
        et_mountings_name = (EditText) findViewById(R.id.et_mountings_name);
        et_apartment_type = (EditText) findViewById(R.id.et_apartment_type);
        et_goods_name = (EditText) findViewById(R.id.et_goods_name);

        //
        hasNum = (TextView) findViewById(R.id.hasNum);
        hasSum = (TextView) findViewById(R.id.hasSum);
        findViewById(R.id.rl_datum).setOnClickListener(this);//资料领取
        findViewById(R.id.rl_summarize).setOnClickListener(this);//小结
        sw_bathroom = (SwitchView) findViewById(R.id.sw_bathroom);//整体浴室
        sw_apartment = (SwitchView) findViewById(R.id.sw_apartment);//公寓
        sw_mountings = (SwitchView) findViewById(R.id.sw_mountings);//配件
        sw_kitchen = (SwitchView) findViewById(R.id.sw_kitchen);//整体厨房
        sw_else = (SwitchView) findViewById(R.id.sw_else);//其他

        findViewById(R.id.rl_juyi).setOnClickListener(this);
        findViewById(R.id.rl_lvyi).setOnClickListener(this);
        findViewById(R.id.rl_heyi).setOnClickListener(this);

        iv_img1 = (ImageView) findViewById(R.id.iv_img1);
        iv_img2 = (ImageView) findViewById(R.id.iv_img2);
        iv_img3 = (ImageView) findViewById(R.id.iv_img3);

        sw_bathroom.setOpened(false);
        sw_bathroom.setOnStateChangedListener(this);
        sw_apartment.setOpened(false);
        sw_apartment.setOnStateChangedListener(this);
        sw_mountings.setOpened(false);
        sw_mountings.setOnStateChangedListener(this);
        sw_kitchen.setOpened(false);
        sw_kitchen.setOnStateChangedListener(this);
        sw_else.setOpened(false);
        sw_else.setOnStateChangedListener(this);

        ll_bathroom = (LinearLayout) findViewById(R.id.ll_bathroom);
        rl_apartment = (RelativeLayout) findViewById(R.id.rl_apartment);
        rl_mountings = (RelativeLayout) findViewById(R.id.rl_mountings);
        rl_kitchen = (RelativeLayout) findViewById(R.id.rl_kitchen);
        rl_else = (RelativeLayout) findViewById(R.id.rl_else);

        ll_bathroom.setVisibility(View.GONE);
        rl_apartment.setVisibility(View.GONE);
        rl_mountings.setVisibility(View.GONE);
        rl_kitchen.setVisibility(View.GONE);
        rl_else.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //提交
            case R.id.submit://MyToast("提交");
                setSubmit();
                break;

            //资料领取
            case R.id.rl_datum:
                intent = new Intent(this, GetDatumActivity.class);
                ArrayList<FeedBackBean.InfoEntity> info1 = (ArrayList<FeedBackBean.InfoEntity>) this.info1;
                intent.putExtra("info1", info1);
                startActivityForResult(intent, 998);
                break;

            //小结
            case R.id.rl_summarize:
//                MyToast("小结");
                intent = new Intent(this, SummarizeActivity.class);
                intent.putExtra("remark", conclusion);
                startActivityForResult(intent, 999);
                break;

            case R.id.rl_juyi:
                iv_img1.setImageResource(R.mipmap.ic_choose_li);
                iv_img2.setImageResource(R.mipmap.ic_choose_nor);
                iv_img3.setImageResource(R.mipmap.ic_choose_nor);
                ztys_class = "居逸";
                break;

            case R.id.rl_lvyi:
                iv_img1.setImageResource(R.mipmap.ic_choose_nor);
                iv_img2.setImageResource(R.mipmap.ic_choose_li);
                iv_img3.setImageResource(R.mipmap.ic_choose_nor);
                ztys_class = "旅逸";
                break;

            case R.id.rl_heyi:
                iv_img1.setImageResource(R.mipmap.ic_choose_nor);
                iv_img2.setImageResource(R.mipmap.ic_choose_nor);
                iv_img3.setImageResource(R.mipmap.ic_choose_li);
                ztys_class = "和逸";
                break;

            default:
                break;
        }
    }

    private String conclusion = "";
    private String ztys_class = "居逸";
    private EditText et_bathroom_type, et_kitchen_type, et_mountings_name, et_apartment_type, et_goods_name;

    private void setSubmit() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("info_id", info_id);


        if (sw_bathroom.isOpened()) {
            if ("".equals(et_bathroom_type.getText().toString().trim())) {
                MyToast("请填写浴室型号");
                return;
            }
            map.put("is_ztys", "1");
            map.put("ztys_class", ztys_class);
            map.put("ztys_type", et_bathroom_type.getText().toString().trim());
        } else {
            map.put("is_ztys", "0");
        }
        if (sw_apartment.isOpened()) {
            if ("".equals(et_kitchen_type.getText().toString().trim())) {
                MyToast("请填写公寓型号");
                return;
            }
            map.put("is_gy", "1");
            map.put("gy_type", et_kitchen_type.getText().toString().trim());
        } else {
            map.put("is_gy", "0");
        }
        if (sw_mountings.isOpened()) {
            if ("".equals(et_mountings_name.getText().toString().trim())) {
                MyToast("请填写配件型号");
                return;
            }
            map.put("is_pj", "1");
            map.put("pj_type", et_mountings_name.getText().toString().trim());
        } else {
            map.put("is_pj", "0");
        }
        if (sw_kitchen.isOpened()) {
            if ("".equals(et_apartment_type.getText().toString().trim())) {
                MyToast("请填写整体厨房型号");
                return;
            }
            map.put("is_ztcf", "1");
            map.put("ztcf_type", et_apartment_type.getText().toString().trim());
        } else {
            map.put("is_ztcf", "0");
        }
        if (sw_else.isOpened()) {
            if ("".equals(et_goods_name.getText().toString().trim())) {
                MyToast("请填写其他商品名称");
                return;
            }
            map.put("is_qt", "1");
            map.put("qt_type", et_goods_name.getText().toString().trim());
        } else {
            map.put("is_qt", "0");
        }
        if ("".equals(conclusion)) {
            MyToast("请填写总计");
            return;
        }
        map.put("conclusion", conclusion);
        if (!"".equals(hasNum.getText().toString().trim())) {
            List<FeedBackBean.InfoEntity> info2 = new ArrayList<>();
            info2.clear();
            for (int i = 0; i < info1.size(); i++) {
                if (!"0".equals(info1.get(i).getData_nums())) {
                    info2.add(info1.get(i));
                }
            }
            map.put("data_list", JSON.toJSONString(info2));
        }

        //////////////////////////////////////////
//        showDialog();
//        Set keys = map.keySet();
//        if (keys != null) {
//            Iterator iterator = keys.iterator();
//            while (iterator.hasNext()) {
//                Object key = iterator.next();
//                Object value = map.get(key);
//                Log.e("----" + key, ":" + value + "\n");
//            }
//        }
        XUtil.Post(Url.INFORMATION_ADDFEEDBACK, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        MyToast(info);
                        finish();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    @Override
    public void toggleToOn(View view) {
        switch (view.getId()) {
            case R.id.sw_bathroom:
                sw_bathroom.setOpened(true);
                showOrHidePickLayout1(true);
                break;

            case R.id.sw_apartment:
                sw_apartment.setOpened(true);
                showOrHidePickLayout2(true);
                break;

            case R.id.sw_mountings:
                sw_mountings.setOpened(true);
                showOrHidePickLayout3(true);
                break;

            case R.id.sw_kitchen:
                sw_kitchen.setOpened(true);
                showOrHidePickLayout4(true);
                break;

            case R.id.sw_else:
                sw_else.setOpened(true);
                showOrHidePickLayout5(true);
                break;

            default:
                break;
        }
    }

    @Override
    public void toggleToOff(View view) {
        switch (view.getId()) {
            case R.id.sw_bathroom:
                sw_bathroom.setOpened(false);
                showOrHidePickLayout1(false);
                break;

            case R.id.sw_apartment:
                sw_apartment.setOpened(false);
                showOrHidePickLayout2(false);
                break;

            case R.id.sw_mountings:
                sw_mountings.setOpened(false);
                showOrHidePickLayout3(false);
                break;

            case R.id.sw_kitchen:
                sw_kitchen.setOpened(false);
                showOrHidePickLayout4(false);
                break;

            case R.id.sw_else:
                sw_else.setOpened(false);
                showOrHidePickLayout5(false);
                break;

            default:
                break;
        }
    }

    private void showOrHidePickLayout1(boolean isNeedToPick) {

        if (isNeedToPick) {
            ll_bathroom.setVisibility(View.VISIBLE);
        } else {
            ll_bathroom.setVisibility(View.GONE);
        }
    }

    private void showOrHidePickLayout2(boolean isNeedToPick) {

        if (isNeedToPick) {
            rl_apartment.setVisibility(View.VISIBLE);
        } else {
            rl_apartment.setVisibility(View.GONE);
        }
    }

    private void showOrHidePickLayout3(boolean isNeedToPick) {

        if (isNeedToPick) {
            rl_mountings.setVisibility(View.VISIBLE);
        } else {
            rl_mountings.setVisibility(View.GONE);
        }
    }

    private void showOrHidePickLayout4(boolean isNeedToPick) {

        if (isNeedToPick) {
            rl_kitchen.setVisibility(View.VISIBLE);
        } else {
            rl_kitchen.setVisibility(View.GONE);
        }
    }

    private void showOrHidePickLayout5(boolean isNeedToPick) {

        if (isNeedToPick) {
            rl_else.setVisibility(View.VISIBLE);
        } else {
            rl_else.setVisibility(View.GONE);
        }
    }

    private List<FeedBackBean.InfoEntity> info1 = new ArrayList<>();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == 998) {
                this.info1 = (List<FeedBackBean.InfoEntity>) data.getSerializableExtra("info1");

                int nums = 0;
                for (int i = 0; i < info1.size(); i++) {
                    try {
                        nums += Integer.parseInt(info1.get(i).getData_nums());
                    } catch (Exception e) {

                    }
                }
                if (nums == 0) {
                    hasNum.setText("");
                } else {
                    hasNum.setText("已经领取" + nums + "套");
                }

            } else if (requestCode == 999) {
                conclusion = data.getStringExtra("remark");
                if ("".equals(conclusion)) {
                    hasSum.setText("");
                } else {
                    hasSum.setText("已填写");
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
